package com.sky.controller.user;

import com.github.pagehelper.PageHelper;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.utils.HttpClientUtil;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单业务层
 *
 * @author normal
 * @date 2025年02月28日 下午11:16
 */
@RestController
@RequestMapping("/user/order")
@Api(tags = "C端订单业务相关接口")
@Slf4j
@AllArgsConstructor
public class OrderController {

    private OrderService orderService;

    /**
     * 提交购物车商品订单
     * @param dto
     * @return
     */
    @PostMapping("submit")
    @ApiOperation("提交购物车订单")
    public Result<OrderSubmitVO> submitOrder(@RequestBody OrdersSubmitDTO dto) {
        log.info("提交订单：{}", dto);
        OrderSubmitVO vo = orderService.submitOrder(dto);
        return Result.success(vo);
    }


    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<String> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付:{}", ordersPaymentDTO);
        // 跳过此步 自动提交
//        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
//        log.info("生成预支付交易单:{}", "normal");

        // 自动提交订单
        Map<String, String> map = new HashMap<>();
        map.put("number",ordersPaymentDTO.getOrderNumber());
        HttpClientUtil.doGet("http://localhost:8080/notify/paySuccess",map);

        return Result.success(LocalDateTime.now().toString());
    }

    /**
     * 查询用户历史订单
     *
     * @return 订单分页查询对象
     */
    @ApiOperation("C端-查询历史订单")
    @GetMapping("/historyOrders")
    public Result<PageResult> findHistoryOrder (OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("订单历史信息:{}", ordersPageQueryDTO);
        PageResult page = orderService.pageQueryOrders(ordersPageQueryDTO);
        return Result.success(page);
    }

    /**
     * 取消订单
     *
     * @param id 订单id
     * @return
     */
    @ApiOperation("取消订单")
    @PutMapping("/cancel/{id}")
    public Result cancelOrders(@PathVariable Long id) {
        log.info("取消订单ID[{}]",id);
        orderService.cancelOrders(id);
        return Result.success();
    }

    @ApiOperation("获取订单及其详情信息")
    @GetMapping("/orderDetail/{id}")
    public Result<OrderVO> getOrderWithDetail (@PathVariable Long id){
        log.debug("开始获取完整订单明细，OrderId:{}",id);
        OrderVO orderVO = orderService.getOrderWithDetail(id);
        return Result.success(orderVO);
    }

    /**
     * 再来一单
     *
     * @return 订单ID
     */
    @PostMapping("/repetition/{id}")
    public Result repetition(@PathVariable Long id) {
        orderService.repetitionOrder(id);
        return Result.success();
    }


    /**
     * 催单
     */
    @GetMapping("/reminder/{id}")
    public Result reminder(@PathVariable Long id) {
        orderService.reminder(id);
        return Result.success();
    }




}
